﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Drawing.Drawing2D;
using System.Resources;
using nsGumController.Properties;

namespace nsGumController
{
    public partial class DrawArea : UserControl
    {
        public List<DrawObject> drawList = new List<DrawObject>();
        private Point pointCurrent = new Point(0, 0);
        
        public DrawArea()
        {
            InitializeComponent();
            this.SetStyle(ControlStyles.DoubleBuffer, true);
            this.SetStyle(ControlStyles.UserPaint, true);
            this.SetStyle(ControlStyles.AllPaintingInWmPaint, true);
            this.SetStyle(ControlStyles.ResizeRedraw, true);
        }

        public void AddToDrawList(DrawObject drawObject)
        {
            drawList.Add(drawObject);
        }

        private void DrawArea_Paint(object sender, PaintEventArgs e)
        {
            Graphics g = e.Graphics;
            SolidBrush brush = new SolidBrush(Color.FromArgb(255, 255, 255));
            g.FillRectangle(brush, this.ClientRectangle);
            g.SmoothingMode = SmoothingMode.AntiAlias;

            // 绘制坐标系
            Pen pen1 = new Pen(Color.Silver, 4);
            pen1.EndCap = LineCap.ArrowAnchor;
            g.DrawLine(pen1, 0, 0, this.Size.Width - 20, 0);
            g.DrawString("x", Font, new SolidBrush(Color.Silver), this.Size.Width - 20, 0);
            g.DrawLine(pen1, 0, 0, 0, this.Size.Height-20);
            g.DrawString("y", Font, new SolidBrush(Color.Silver), 0, this.Size.Height - 20);

            // 绘制说明信息
            pen1.Color = Color.Black;
            pen1.Width = 2;
            pen1.DashStyle = DashStyle.Dash;
            g.DrawLine(pen1, this.Size.Width - 100, this.Size.Height - 75, this.Size.Width - 80, this.Height - 75);
            g.DrawString(":非插补移动", Font, new SolidBrush(Color.Silver), this.Size.Width - 75, this.Size.Height - 80);
            pen1.DashStyle = DashStyle.Solid;
            g.DrawLine(pen1, this.Size.Width - 100, this.Size.Height - 60, this.Size.Width - 80, this.Height - 60);
            g.DrawString(":插补移动", Font, new SolidBrush(Color.Silver), this.Size.Width - 75, this.Size.Height - 65);
            pen1.Width = 4;
            pen1.DashStyle = DashStyle.Dash;
            g.DrawLine(pen1, this.Size.Width - 100, this.Size.Height - 45, this.Size.Width - 80, this.Height - 45);
            g.DrawString(":Z轴压下", Font, new SolidBrush(Color.Silver), this.Size.Width - 75, this.Size.Height - 50);
            pen1.Color = Color.Orange;
            pen1.DashStyle = DashStyle.Solid;
            g.DrawLine(pen1, this.Size.Width - 100, this.Size.Height - 30, this.Size.Width - 80, this.Height - 30);
            g.DrawString(":点胶开启", Font, new SolidBrush(Color.Silver), this.Size.Width - 75, this.Size.Height - 35);
            pen1.Color = Color.Green;
            pen1.Width = 1;
            g.FillEllipse(new SolidBrush(Color.Green), this.Size.Width - 95, this.Size.Height - 20, 8, 8);
            g.DrawString(":原点", Font, new SolidBrush(Color.Silver), this.Size.Width - 75, this.Size.Height - 20);
            
            // 绘制Action动作图形
            if ((drawList == null) || (drawList.Count == 0))
            {
                return;
            }
            foreach (DrawObject drawObject in drawList)
            {
                drawObject.Draw(g);
            }

            // 绘制当前坐标点
            ResourceManager rm = new ResourceManager("nsGumController.Properties.Resources", System.Reflection.Assembly.GetExecutingAssembly());
            g.DrawIcon((Icon)rm.GetObject("PointCurrent"), pointCurrent.X-8, pointCurrent.Y-8);

            brush.Dispose();
        }

        private void DrawArea_MouseDown(object sender, MouseEventArgs e)
        {
            if (e.Button == MouseButtons.Left)
            {
                Point point = e.Location;
                foreach (DrawObject drawObject in drawList)
                {
                    if (drawObject.IsPointInObject(point))
                    {
                        drawObject.selected = true;
                    }
                    else
                    {
                        drawObject.selected = false;
                    }
                    //drawObject.Draw(this.CreateGraphics());
                }
            }
            Refresh();
        }

        public void SetCurrentPoint(int x, int y)
        {
            pointCurrent.X = x;
            pointCurrent.Y = y;
        }
    }
}
